package com.carful.xigua.modules.common.config;


import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Configuration
public class RestTemplateConfig {
    @Bean
    public RestTemplate restTemplate(){
        RestTemplate restTemplate = new RestTemplate();

        restTemplate.getMessageConverters().add(new WxMappingJackson2HttpMessageConverter());
        return restTemplate;
    }

    class CustomJacksonHttpMessageConverter extends MappingJackson2HttpMessageConverter {
        public CustomJacksonHttpMessageConverter() {
            this(Jackson2ObjectMapperBuilder.json().build());
        }

        public CustomJacksonHttpMessageConverter(ObjectMapper objectMapper) {
            super(objectMapper);
            // 这里是重点，增加支持的类型，看你的情况加
            // 我这里目前只需要加个 TEXT/PLAIN
            setSupportedMediaTypes(Arrays.asList(MediaType.TEXT_PLAIN,
                    MediaType.APPLICATION_JSON,
                    new MediaType("application", "*+json")));
        }
    }

    class WxMappingJackson2HttpMessageConverter extends MappingJackson2HttpMessageConverter {
        public WxMappingJackson2HttpMessageConverter(){
            List<MediaType> mediaTypes = new ArrayList<>();
            mediaTypes.add(MediaType.TEXT_PLAIN);
            setSupportedMediaTypes(mediaTypes);// tag6
        }
    }
}
